package com.imooc.o2o.web.shopadmin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.imooc.o2o.service.ShopService;
@Controller
@RequestMapping(value="/shopadmin" ,method= {RequestMethod.GET,RequestMethod.POST})
public class ShopAdminController {
	@Autowired
	private ShopService shopService;

	@RequestMapping(value="/shopoperation")
	public String shopOperation() {
		return "shop/shopoperation";
	}
	@RequestMapping(value="/shoplist")
	public String shopList() {
		return "shop/shoplist";
	}
	@RequestMapping(value="/shopmanage")
	public String shopManagement() {
		return "shop/shopmanage";
	}
	@RequestMapping(value="/productcategorymanage")
	public String productCategoryManage() {
		return "shop/productcategorymanage";
	}
	@RequestMapping(value="/productoperation")
	public String productOperation() {
		return "shop/productoperation";
	}
	@RequestMapping(value="/productmanage")
	public String productManage() {
		return "shop/productmanage";
	}
//	@RequestMapping(value="/registershop", method=RequestMethod.POST)
//	@ResponseBody
//	private Map<String,Object> registerShop(HttpServletRequest request){
//		Map<String,Object> modelMap = new HashMap<String,Object>();
//		String shopStr = HttpServletRequestUtil.getString(request, "shopStr");
//		ObjectMapper mapper = new ObjectMapper();
//		Shop shop = null;
//		try {
//			shop = mapper.readValue(shopStr, Shop.class);
//		}catch(Exception e){
//			modelMap.put("success",false);
//			modelMap.put("errMsg", e.getMessage());
//			return modelMap;
//		}
//		CommonsMultipartFile shopImg = null;
//		CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(
//				request.getSession().getServletContext());
//		if(commonsMultipartResolver.isMultipart(request)) {
//			MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest)request;
//			shopImg = (CommonsMultipartFile)multipartHttpServletRequest.getFile("shopImg");
//		}else {
//			modelMap.put("success",false);
//			modelMap.put("errMsg", "上传图片不能为空");
//			return modelMap;
//		}
//		//注册店铺
//		if(shop != null && shopImg != null) {
//			PersonInfo owner = new PersonInfo();
//			owner.setUserId(1L);
//			shop.setOwner(owner);
//			
//			ShopExecution se;
//			try {
//				se = shopService.addShop(shop,shopImg.getInputStream(), shopImg.getOriginalFilename());
//				if(se.getState() == ShopStateEnum.CHECK.getState()) {
//					modelMap.put("success",true);
//				}else {
//					modelMap.put("success",false);
//					modelMap.put("errMsg", se.getStateInfo());
//				}
//			} catch (RuntimeException e) {
//				modelMap.put("success",false);
//				modelMap.put("errMsg",  e.getMessage());
//			} catch (IOException e) {
//				modelMap.put("success",false);
//				modelMap.put("errMsg",  e.getMessage());
//			}
//			
//			return modelMap;
//		}else {
//			modelMap.put("success",false);
//			modelMap.put("errMsg", "请输入店铺信息");
//			return modelMap;
//		}
//	}
	
	
//	private static void inputStreamToFile(InputStream ins,File file) {
//		FileOutputStream os = null;
//		try {
//			os = new FileOutputStream(file);
//			int bytesRead = 0;
//			byte[] buffer = new byte[1024];
//			while((bytesRead = ins.read(buffer)) != -1) {
//				os.write(buffer, 0, bytesRead);
//			}
//		}catch(Exception e){
//			throw new RuntimeException("调用InputStreamToFile产生异常："+e.getMessage());
//			
//		}finally {
//			try {
//				if(os!=null) {
//					os.close();
//				}
//				if(ins!= null) {
//					ins.close();
//				}
//			}catch(IOException e) {
//				throw new RuntimeException("InputStreamToFile关闭io产生异常："+e.getMessage());
//			}
//		}
//	}
}
